<?
class String{
    static function display_sort_title($str,$word_number){
        $c = str_word_count($str);
        $array1=array($c);
        $new_str='';
       
        if($c>=$word_number){
            $array1 = explode(" ",$str);
            $i=0;
            while($i<sizeof($array1)){
                if($i<$word_number){
                    $new_str.=$array1[$i].' ';
                }
                $i++;
            }
            return $new_str.'...';
        }
        else{
            return $str;   
        }
    }
   
    static function string2js($st){
        return strtr($st, array('\''=>'\\\'','\\'=>'\\\\','\n'=>'',chr(10)=>'\\
',chr(13)=>''));
    }
   
    //###################################################################################
    //added from rongbay string 23-09-2011 by HieuTV
	static function word_limit($string, $length, $ellipsis="...") {
	  return (count($words = explode(' ', $string)) > $length) ? implode(' ', array_slice($words, 0, $length)) . $ellipsis : $string;
	}

	static function cutoff($strContent, $intLength){
		$arrTemp= split(" ",$strContent);
		$strTemp="";
		if(count($arrTemp)>$intLength){
			for($i=0;$i<$intLength;$i++){
				$strTemp .= $arrTemp[$i]." ";
			}
			$strTemp.= "...";
		}
		else{
			$strTemp = $strContent;
		}
		return $strTemp;
	}

	/*	Ham remove html va php tag 
			input : chuoi ki tu can remove tag,tag cho phep hien thi
			output : chuoi ky tu da duoc xu ly.
	*/
	static function find_img($text, $type= 2){

		@preg_match_all("#src=\"(.*?)\"#", $text, $img_matches);

		srand((float) microtime() * 10000000);

		$count = @count($img_matches[1]) - 1;

		if($count < 1){$count = 0;}

		$rand = @rand(0, $count);
		
		//$rand_keys = @array_rand($img_matches[1], 2);

		if($type=='2'){
		$imgout = $img_matches[1][$rand];
		}
		else{
		$imgout = $img_matches[1][0];
		}
	  return $imgout;
	}

	static function _name_cleaner($name,$replace_string=""){
		return preg_replace( "/[^a-zA-Z0-9\-\_]/", $replace_string , $name );
	}

	static function make_safe_name($name,$replace_string=""){
		return preg_replace( "/[^\w\.]/", $replace_string , $name );
	}
	
	static function safe_title($text){
		$text = Unicode::convert_encoding("utf8", "none", $text);
		$text = self::_name_cleaner($text,"-");
		$text = str_replace("----","-",$text);
		$text = str_replace("---","-",$text);
		$text = str_replace("--","-",$text);
		$text = trim($text, '-');				 				
		return $text;
	}

    //added by Nova....29.09.08 xu ly cac ky tu dac biet ngoai trang list
    static function filter_title($str) {
        $str = HTML::post_db_parse_html($str);
        $filter = "\$|,,|@|\!|#|~|`|\*|\^|\&|\(|\)|\+|\=|\[|\-|\_|\]|\[|\}|\{|\;|\'|\"|\<|\>|\?|\||\\|\.\.|►|◄|♥|♣|♫|♩|©|∞|æ|Æ|å|Å|≈|Ä|ä|„|β|¦|•|∩|Ç|ç|¸|¢|χ|ˆ|♣|©|¤|†|‡|↓|°|Δ|δ|♦|÷|ε|≡|η|ð|ë|Ë|€|ƒ|½|¼|¾|⁄|Γ|γ|≥|>|↔|…|î|Î|∞|∫|ι|¿|Ï|ï|κ|λ|Λ|«|←|“|”|≤|◊|‹|‘|’|<|¯|—|µ|·|μ|–|≠|¬|ñ|Ñ|ν|Œ|œ|‾|ω|Ω|ª|º|Ø|ø|Ö|ö|¶|∂|‰|φ|Φ|Π|π|ϖ|±|£|∏|ψ|Ψ|√|»|→|®|ρ|›|Š|š|§|Σ|σ|ς|♠|∑|¹|²|³|ß|τ|Θ|θ|ϑ|Þ|þ|˜|×|™|↑|û|Û|¨|ϒ|υ|ü|Ü|ξ|Ξ|¥|ÿ|Ÿ|ζ|██▓▒░|█|xxx|XXX|aaaa|AAAA|██|██▬▬▬|▬▬▬";

        //æ|Æ||å|Å|≈|Ä|ä|„|β|¦|•|∩|Ç|ç|¸|¢|χ|ˆ|♣|©|¤|†|‡|↓|°|Δ|δ|♦|÷|ε|≡|η|ð|ë|Ë|€|ƒ|½|¼|¾|⁄|Γ|γ|≥|>|↔|…|î|Î|∞|∫|ι|¿|Ï|ï|κ|λ|Λ|«|←|“|≤|◊|‹|‘|<|¯|—|µ|·|μ|–|≠|¬|ñ|Ñ|ν|Œ|œ|‾|ω|Ω|ª|º|Ø|ø|Ö|ö|¶|∂|‰|φ|Φ|Π|π|ϖ|±|£|∏|ψ|Ψ|√|»|→|”|®|ρ|›|’|Š|š|§|Σ|σ|ς|♠|∑|¹|²|³|ß|τ|Θ|θ|ϑ|Þ|þ|˜|×|™|↑|û|Û|¨|ϒ|υ|ü|Ü|ξ|Ξ|¥|ÿ|Ÿ|ζ

        $str = preg_replace("/$filter/is", " ", $str);
        return preg_replace('/^([%|\$|\s])+/', '', $str);
    }
    
	//-----------------------------------------
	// Word wrap, wraps 'da word innit
	//-----------------------------------------
	static function my_wordwrap($t="", $chrs=0, $replace="<br />"){
		if ( $t == "" ){
			return $t;
		}
		
		if ( $chrs < 1 ){
			return $t;
		}
		
		$t = preg_replace("#([^\s]{".$chrs."})#i", " \\1".$replace ,$t);
		
		return $t;
	}

	// Check mobile number and convert to 84xxx - Add by KBY 19/11
	static function checkMobile($number){
		if($number{0}=="0" && is_numeric($number))
		{
			$number = "84". substr($number,1);
		}
		elseif($number{0}.$number{1}=="84" && is_numeric($number))
		{
		}	
		else 
		{
			$number = "error";
		}
		
		return $number;
	}
	
	static function number($number){
		return number_format($number, null,null, '.');
	}
    
	//remove ký tự đặc biệt
	static function remove_special_sybol($test){
		$arr1=array("~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "+", "=", "|", "\\", "{", "}",":", ";", "<", ">", "/", "?");
		$arr2=array("");
		if(!$test){
			return $test;
		} else {
			$new_test=str_replace($arr1,$arr2,$test);
			return $new_test;
		}
	}
    
    static function trimSpace($str="")
    {
        if ($str == "") {
            return;
        }
        
        $str = str_replace("&nbsp;", " ", $str);
        $str = preg_replace('![\t ]*[\r\n]+[\t ]*!', ' ', $str);
        return trim($str);
        return trim(preg_replace('/[[:space:]]+/i', " ", trim($str)));       
    }
    
	static function make_single_space($t=""){		
		return String::trimSpace($t);
	}
	
    static function delDoubleSpace($str) {
    	return $str;
        if (preg_match('  ',' ', $str)) {
            $str = preg_replace('  ', ' ', $str);
            
            $str = self::delDoubleSpace($str);
            return $str;
        } else {
            return $str;
        }
    }	
    
	static function convert_one_n($text){	
		$text = str_replace("\n\n\n\n\n","\n",$text);
		$text = str_replace("\n\n\n\n","\n",$text);
		$text = str_replace("\n\n\n","\n",$text);
		$text = str_replace("\n\n","\n",$text);
		$text = trim($text, '\n');
					
					
		return $text;
	}
}
?>